package com.pdsu.graduation_design.views;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.MainActivityBarAdapter;
import com.pdsu.graduation_design.adapter.MainActivityListViewAdapter;
import com.pdsu.graduation_design.domain.Commodity;
import com.pdsu.graduation_design.domain.CommodityType;
import com.pdsu.graduation_design.listener.MainActivityBarListener;
import com.pdsu.graduation_design.listener.MainActivityListViewListener;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.widget.TabView;

public class MainGridViewItemDetailActivity extends AppCompatActivity {


    //数据
    private List<CommodityType> commodityTypes;

    //点击项
    private int index;

    private Context context;


    /**
     * 获取同一类别数据填充到
     * 1. 垂直导航
     * 2.listview
     * 中
     * @param savedInstanceState
     */

    private List<String> menu_types;

    //main activity 垂直导航栏
    private VerticalTabLayout activity_main_bar;


    //导航栏旁边的ListView
    private ListView activity_main_listview;


    private Button add_btn,delete_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_grid_view_item_detail);
        try {
            init();
            bind();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private void init() throws Exception {
        context=this;
        String commodityTypesjson=getIntent().getStringExtra("commodityTypesjson");
        commodityTypes = JsonUtil.json2list(commodityTypesjson,CommodityType.class);
        index=getIntent().getIntExtra("index",-1);
        activity_main_bar=findViewById(R.id.activity_main_bar);
        activity_main_listview=findViewById(R.id.activity_main_listview2);
        menu_types=new ArrayList<>();
        menu_types.add("one");
        menu_types.add("one2");


    }



    private void bind() {

        /**
         *  沃日他dei  23点21分 ....
         *
         */
//        activity_main_bar.getTabAt(0).setChecked(false);
//        activity_main_bar.getTabAt(0).setSelected(false);
//        activity_main_bar.getTabAt(index).setPressed(true);
//        activity_main_bar.getTabAt(index).setSelected(true);
//        activity_main_bar.getTabAt(index).setSelected(true);
//        activity_main_bar.getTabAt(index).setChecked(true);
        //
        activity_main_bar.setTabAdapter(new MainActivityBarAdapter(commodityTypes));
        activity_main_bar.setTabSelected(index);
        if (index==0){
            RequestParams requestParams = new RequestParams(MainActivity.URL+"/commodity/getCommodityByCTID");
            requestParams.addParameter("ctid",commodityTypes.get(0).getCtid());
            x.http().post(requestParams, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {

                    try {
                        Result r=JsonUtil.JsonToObj(result,Result.class);
                        if (r.getCode().equals("200")){
                            List<Commodity> commodities =JsonUtil.json2list(r.getData().toString(),Commodity.class);
                            //给右侧栏目添加适配
                            activity_main_listview.setAdapter(new MainActivityListViewAdapter(context,commodities));

                            activity_main_listview.setOnItemClickListener(new MainActivityListViewListener(getApplicationContext(),commodities));

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }
                @Override
                public void onCancelled(CancelledException cex) {
                }
                @Override
                public void onFinished() {
                }
            });

        }
        activity_main_bar.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                //获取当前type的tid 查询commodity表
                RequestParams requestParams = new RequestParams(MainActivity.URL+"/commodity/getCommodityByCTID");
                requestParams.addParameter("ctid",commodityTypes.get(position).getCtid());
                x.http().post(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {

                        try {
                            Result r=JsonUtil.JsonToObj(result,Result.class);
                            if (r.getCode().equals("200")){
                                List<Commodity> commodities =JsonUtil.json2list(r.getData().toString(),Commodity.class);
                                //给右侧栏目添加适配
                                activity_main_listview.setAdapter(new MainActivityListViewAdapter(context,commodities));

                                activity_main_listview.setOnItemClickListener(new MainActivityListViewListener(getApplicationContext(),commodities));

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                    }
                    @Override
                    public void onCancelled(CancelledException cex) {
                    }
                    @Override
                    public void onFinished() {
                    }
                });


            }
            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });

        //根据当前选中的导航栏type，查询该类型的菜


    }


}